/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysLoginService.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月22日
 */
package com.sirdc.modules.sys.service;

import java.util.ArrayList;
import java.util.List;

import com.sirdc.modules.sys.dao.SysUserDao;
import com.sirdc.modules.sys.entity.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.exceptions.UserExistException;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysLoginDao;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysLoginFilter;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.util.PasswordHelper;
import com.sirdc.modules.sys.util.RedisUtils;
import com.sirdc.modules.sys.util.SysGlobals;
import com.sirdc.modules.sys.util.SysGlobals.AccountStates;

/**
 *
 *
 */
@Service
public class SysLoginService extends StringPKService<SysLogin> {

    @Autowired
    private SysLoginDao sysLoginDao;
    @Autowired
    private SysUserDao sysUserDao;
    //	@Autowired
//	private SysUserPermissionDao sysUserPermissionDao;
    @Autowired
    private PasswordHelper passwordHelper;

    @Override
    protected BaseDao<SysLogin, String> getDao() {
        return sysLoginDao;
    }

    public List<SysLogin> query(SysLoginFilter filter) {
        return sysLoginDao.query(filter);
    }

    @Override
    protected void beforeSave(SysLogin user) {
        user.setState(RedisUtils.getDictVal(SysGlobals.ACCOUNTSTATE, AccountStates.NOMAL, "name"));//初始化帐号状态
        //user.setPassword(RedisUtils.getDictVal(SysGlobals.INITPASSWORD, SysGlobals.BIGCLASS, "attr1"));//初始化密码
        passwordHelper.encryptPassword(user);//密码加密

        //判断用户是否已经存在
        if (isExistForUsername(user.getUsername())) {
            throw new UserExistException(user.getUsername());
        }
    }

    /**
     * 根学号判断用户是否存在，存在返回ture
     *
     * @param sno
     * @return
     * @author: huiyang.yu
     */
    public boolean isExistForSno(String sno) {
        SysLogin sysLogin = sysLoginDao.getLoginBySno(sno);
        if (sysLogin == null) {
            return false;
        }
        return true;
    }

    /**
     * 根据用户的口令信息获取用户信息
     *
     * @param token
     * @return
     * @author: weihuang.peng
     */
    public SysLogin getUserByLoginName(String token) {
        return sysLoginDao.getUserByLoginName(token);
    }

    /**
     * 根据电话号码判断用户是否存在
     *
     * @param mobile
     * @return
     * @author: weihuang.peng
     */
    public boolean isExistForMobile(String mobile) {
        SysLogin sysLogin = sysLoginDao.getLoginByMobile(mobile);
        if (sysLogin == null) {
            return false;
        }
        return true;
    }

    /**
     * 根据邮箱判断用户是否存在
     *
     * @param mail
     * @return
     * @author: weihuang.peng
     */
    public boolean isExistForEmail(String mail) {
        SysLogin sysLogin = sysLoginDao.getLoginByEmail(mail);
        if (sysLogin == null) {
            return false;
        }
        return true;
    }

    /**
     * 更新其他信息
     */
    public void updateLogin(SysUserDataBean databean) {
        //保存登陆信息
        SysLogin login = getById(databean.getUserId());
        if (databean.getDeptId() != null) {
            login.setDeptId(databean.getDeptId());
        }
        if (databean.getRoleId() != null) {
            login.setRoleId(databean.getRoleId());
        }

        if ((databean.getPassword() != null) && (!login.getPassword().equals(databean.getPassword()))) {
            login.setPassword(databean.getPassword());
            passwordHelper.encryptPassword(login);
        }
        merge(login);
    }

    /**
     * 返回加密后的新密码
     *
     * @param password
     * @param salt
     * @return
     */
    public String getEncryPass(String password, String salt) {
        return passwordHelper.encryptPassResult(password, salt);
    }

    /**
     * 更新密码
     *
     * @param databean
     * @author: weihuang.peng
     */
    public void updatePassword(SysUserDataBean databean) {
        SysLogin login = getById(databean.getUserId());
        login.setPassword(databean.getPassword());
        /*if (!login.getPassword().equals(databean.getOldPassword())) {

        }*/
        passwordHelper.encryptPassword(login);
        sysLoginDao.update(login);
    }

    /**
     * @param filter
     * @author: weihuang.peng
     */
    public List<String> queryLoginByRoleDept(SysUserFilter filter) {
        return sysLoginDao.queryLoginByRoleDept(filter);
    }

    public List<SysLogin> queryUserLoginByRoleDept(SysUserFilter filter) {
        return sysLoginDao.queryUserLoginByRoleDept(filter);
    }

    /*
    下级人员
     */
    public List<SysUser> queryLowerList(SysUser user){
        SysLogin login = this.getById(user.getSysId());
        List<SysLogin>lowerList= sysLoginDao.queryLowerByRoleDept(login.getDeptId(),login.getRoleId());
        List<SysUser>userList=new ArrayList<>();
        for (SysLogin sysLogin : lowerList) {
            SysUser user1 = sysUserDao.getById(sysLogin.getSysId());
            if ("03".equals(user.getState())) {
                continue;
            }
            userList.add(user1);
        }
        return userList;
    }

    public List<SysLogin> queryLowerByRoleDept (String deptId,String roleId){
        return sysLoginDao.queryLowerByRoleDept(deptId, roleId);
    }
    /**
     * @param userId
     * @param roleId
     * @param deptId
     * @return
     * @author: weihuang.peng
     */
    public boolean isExistUserPermission(String userId, String roleId, String deptId) {
        return sysLoginDao.isExistUserPermission(userId, roleId, deptId);
    }

    public boolean isExistForUsername(String username) {
        SysLogin sysLogin = sysLoginDao.getLoginByUsername(username);
        if (sysLogin == null) {
            return false;
        }
        return true;
    }


    public boolean isExistForUsernameDept(String username,String deptId) {
        SysLogin sysLogin = sysLoginDao.getLoginByUsernameDept(username,deptId);
        if (sysLogin == null) {
            return false;
        }
        return true;
    }
    public List<SysLogin> queryLeaderList(String deptId,String roleId) {
        return sysLoginDao.queryLeaderList(deptId,roleId);
    }

    public List<SysLogin> getByNameAndRoleId(String name, String roleId) {

        return sysLoginDao.getByNameAndRoleId(name,roleId);
    }

    public List<SysLogin> getByUsernameDept(String s, String deptId) {
        return sysLoginDao.getByUsernameDept(s,deptId);
    }

    public void batchDeleteLogin(List<String> ids) {
        sysLoginDao.batchDeleteLogin(ids);
    }

    public List<SysLogin> queryList() {
        return sysLoginDao.queryList();
    }

    public List<SysLogin> queryCountyleader() {
        return sysLoginDao.queryCountyleader();
    }

    public List<SysUserDataBean> getByDeptId(String deptId) {
        List<SysLogin>logins= sysLoginDao.queryLowerByRoleDept(deptId,"01");
        List<SysUserDataBean> dataBeans=new ArrayList<>();
        for (SysLogin login : logins) {
            SysUserDataBean userDataBean = new SysUserDataBean();
            SysUser user = sysUserDao.getById(login.getSysId());
            userDataBean.setUserId(login.getSysId());
            userDataBean.setName(user.getName());
            userDataBean.setRoleId(login.getRoleId());
            userDataBean.setDeptId(login.getDeptId());
            userDataBean.setPosition(user.getPosition());
            userDataBean.setMobile(user.getMobile());
            userDataBean.setFloorId(user.getFloorId());
            userDataBean.setDemo(user.getDemo());
            dataBeans.add(userDataBean);
        }
        return dataBeans;
    }
}
